package org.iteye.bbjava.util.interpreter;

import org.dom4j.Element;
import org.dom4j.Node;

public class NonTerminalExpression extends AbstractExpression {

	Context ctx;
	
	public NonTerminalExpression(Context ctx){
		this.ctx = ctx;
	}
	
	public NonTerminalExpression(){
		
	}

	public void setContext(Context ctx) {
		this.ctx = ctx;
	}


	@Override
	public String[] interpret() {
		
		if(null == this.ctx )
			return null;
		
		Node node =	this.ctx.getNodeByElementName("entity");
		String []entityName = new String[1];
		entityName[0] = node.getText();
		
		return entityName;
		

	}
	
	public String createFinalResult(String type){
		
		StringBuffer sb = new StringBuffer();
		if(null != type && !"".equals(type)){
			Node node = ctx.queryElement("//attribute");
			Element elem = (Element)node;
			if(type.equals("createInterface")){
				String[] name = this.interpret(); 
				
				sb.append("public void save( " + name[0] + " entity); ");
				sb.append("public " +  name[0] + " queryEntityById( " +
						elem.attribute("javaType").getText() + " " + elem.attribute("name").getText() + "); ");
				sb.append("public void update( " + name[0] + " entity); ");
				sb.append("public int delete( " + elem.attribute("javaType").getText() + " "
						+ elem.attribute("name").getText() + "); ");
			}
			if(type.equals("createImplementClass")){
				String[] name = this.interpret(); 
				
				sb.append("public void save( " + name[0] + " entity){//todo } ");
				sb.append("public " +  name[0] + " queryEntityById( " + 
						elem.attribute("javaType").getText() + " " + elem.attribute("name").getText() + "){//todo } ");
				sb.append("public void update( " + name[0] + " entity){//todo } ");
				sb.append("public int delete( " + elem.attribute("javaType").getText() + " " + 
						elem.attribute("name").getText() + "){//todo } ");
			}
		
		}
		return sb.toString();
		
	}

}
